package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.dao.UserMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.utils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ArticleReplyServiceImpl
 * @Description TODO
 * @Author H.
 * @Date 2023/6/20 14:09
 * @Version 1.0
 */
@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        if(articleId == null){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用方法
        List<ArticleReply> results = articleReplyMapper.selectByArticleId(articleId);
        //返回结果
        return results;
    }

    @Override
    public void create(ArticleReply articleReply) {
        // 非空校验
        if (articleReply == null || articleReply.getPostUserId() == null
                || articleReply.getArticleId() == null || StringUtils.isEmpty(articleReply.getContent())) {
            // 记录日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验帖子状态
        Article article = articleMapper.selectByPrimaryKey(articleReply.getArticleId());
        if (article == null || article.getState() == 1 || article.getDeleteState() == 1) {
            // 记录日志
            log.warn(ResultCode.FAILED_STATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_STATE));
        }

        // 校验用户状态
        User user = userMapper.selectByPrimaryKey(articleReply.getPostUserId());
        if (user == null || user.getState() == 1 || user.getDeleteState() == 1) {
            // 记录日志
            log.warn(ResultCode.FAILED_STATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_STATE));
        }

        // 设置默认值
        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        // 定义回复信息
        int articleReplyRow = articleReplyMapper.insertSelective(articleReply);
        if (articleReplyRow != 1) {
            log.warn(ResultCode.ERROR_INSERT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_INSERT.toString()));
        }

        // 更新帖子的回复数量
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount() + 1);
        // 调用更新方法
        int articleRow = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (articleRow != 1) {
            log.warn(ResultCode.ERROR_INSERT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_INSERT.toString()));
        }
        // 记录日志
        log.info("回复发表成功, 帖子id = " + articleReply.getArticleId());
    }
 }
